'use strict';

const keyContrulor = require('./../lib/keyContrulor');

const Controller = require('egg').Controller;
const { creatToken, parseToken } = require('./../lib/createToken');

class UserController extends Controller {
  async index() {
    const { ctx } = this;
    // ctx.body = 'hi, user';
    ctx.body = await ctx.model.User.find();
  }

  // 商家login
  async login() {
    const { ctx } = this;
    const { body = {} } = ctx.request;
    let { phone, password, token } = body;
    // 验证token；
    if (!phone && !password && token) {
      const userInfo = parseToken(token);
      phone = userInfo.phone;
      password = userInfo.password;
    }
    if (!phone && !password) {
      ctx.body = {
        success: false,
        msg: '无效的请求！',
        data: null,
      };
      return;
    }
    // 找一下有没有这个用户
    const findItem = await ctx.model.User.findOne({
      phone,
    });
    if (!findItem) {
      ctx.body = {
        success: false,
        msg: '该手机号没有注册用户！',
        data: null,
      };
      return;
    }

    let real_password = password;
    if (!token) {
      // 解密
      real_password = await keyContrulor.decrypt(password);
    }
    if (findItem.password !== real_password) {
      ctx.body = {
        success: false,
        msg: '登录密码错误',
        data: null,
      };
      return;
    }
    const res = {
      ...findItem,
    };

    // 生成token;
    const new_token = creatToken({
      phone,
      password,
    });
    delete res.password;
    ctx.body = {
      success: true,
      msg: '',
      data: {
        userInfo: res._doc || res,
        token: new_token,
      },
    };
  }

  async registerAdmin() {
    const { ctx } = this;
    const { body = {} } = ctx.request;
    const { name, password, phone, email, sex = 0, age = 0, lv } = body;
    if (!name || !password || !phone || !email || ![ 0, 1, 2 ].includes(lv)) {
      ctx.body = {
        success: false,
        msg: 'name, password, phone, email,lv都是必传字段！',
        data: null,
      };
      return;
    }
    // 校验手机号唯一
    const findOneByPhone = await ctx.model.User.findOne({
      phone,
    });
    if (!findOneByPhone) {
      // 通过校验
      // 解密字段
      const real_pw = await keyContrulor.decrypt(password);
      // 用户数据入库
      await ctx.model.User.create({
        name,
        phone,
        email,
        sex,
        age,
        lv,
        password: real_pw,
      });
      ctx.body = {
        success: true,
        msg: '注册成功',
        data: null,
      };
      return;

    }
    // 已存在手机号相同的用户数据
    ctx.body = {
      success: false,
      msg: '已存在手机号相同的用户数据！',
      data: null,
    };
    return;

  }
  // 获取管理员列表
  async getUserList() {
    const { ctx } = this;
    const data = await ctx.model.User.find();
    ctx.body = {
      success: true,
      msg: '',
      data,
    };
  }
  // 创建一个管理员
  async createUser() {
    const { ctx } = this;
    const { body = {} } = ctx.request;
    const { name, phone, password, lv, sex } = body;
    if (!name || !phone || !password) {
      ctx.body = {
        success: false,
        msg: '搞的不太行',
        data: null,
      };
    }
    const _id = await ctx.model.User.create({
      name,
      phone,
      password,
      lv,
      sex,
    });
    ctx.body = {
      success: true,
      msg: '',
      data: _id,
    };
  }
}

module.exports = UserController;
